<template>
  <div>
    <!-- width使用百分比, max-width使用具体的xxxpx , 用以适配小屏幕的PC端 -->
    <el-dialog
      :title="title"
      v-model="visable"
      width="65%"
      top="15vh"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 850px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 95%;padding-left: 5%;"
        >
        <el-row :gutter="25">
          <el-col :span="12">
            <el-form-item label="手机号:" prop="phone">
              <el-input style="width: 70%;" v-model="dialogFormData.phone" placeholder="请输入手机号"  clearable :disabled="!isBlank(dialogFormData.id)"/>
              <el-button style="width: 25%;margin-left: 5%;" type="primary" @click="getRandomPhone" :disabled="!isBlank(dialogFormData.id)">随机号码</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码:" prop="loginPassword">
              <el-input v-model="dialogFormData.loginPassword" placeholder="请输入登录密码" type="password" show-password clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学员名称:" prop="name">
              <el-input v-model="dialogFormData.name" placeholder="请输入学员名称"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学员代码:" prop="code">
              <el-input v-model="dialogFormData.code" placeholder="请输入学员代码"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信号:" prop="wechatID">
              <el-input v-model="dialogFormData.wechatID" placeholder="请输入微信号"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信昵称:" prop="wechatNickname">
              <el-input v-model="dialogFormData.wechatNickname" placeholder="请输入微信昵称"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自媒体平台: ">
              <el-select
                v-model="dialogFormData.weMediaPlatform"
                placeholder="请选择自媒体平台" 
                
                clearable
              >
                <el-option
                  v-for="item in weMediaPlatformList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自媒体账号: ">
              <el-select
                v-model="dialogFormData.weMediaAccount"
                placeholder="请选择自媒体账号" 
                
                clearable
              >
                <el-option
                  v-for="item in weMediaAccountList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平台粉丝昵称:" prop="platformNickname">
              <el-input v-model="dialogFormData.platformNickname" placeholder="请输入平台粉丝昵称"  clearable />
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="主播:" prop="tvAnchor">
              <el-input v-model="dialogFormData.tvAnchor" placeholder="请输入主播"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="引流人:" prop="recommended">
              <el-select
                v-model="dialogFormData.recommended"
                placeholder="请选择引流人" 
                clearable
                multiple
              >
                <el-option
                  v-for="item in recommendedList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学员经理:" prop="studentManager">
              <el-select
                v-model="dialogFormData.studentManager"
                placeholder="请选择学员经理" 
                multiple
                clearable
              >
                <el-option
                  v-for="item in studentManagerList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="引流日期:" prop="recommendedDate">
              <el-date-picker 
                v-model="dialogFormData.recommendedDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择引流日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号等级:" prop="accountLevel"  style="text-align: left;">
              <el-select
                v-model="dialogFormData.accountLevel"
                placeholder="账号等级" 
                clearable
              >
                <el-option
                  v-for="item in accountLevelList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组别:" prop="groupType"  style="text-align: left;">
              <el-select
                v-model="dialogFormData.groupType"
                placeholder="组别" 
                clearable
              >
                <el-option
                  v-for="item in groupTypeList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号:" prop="account">
              <template #label>
                <el-tooltip
                  class="box-item"
                  effect="dark"
                  content="保证金监控中心账号"
                  placement="bottom-start"
                >
                <span><el-icon style="font-size: 15px;vertical-align: sub;"><QuestionFilled /></el-icon>账号:</span>
                </el-tooltip>
              </template>
              <el-input v-model="dialogFormData.account" placeholder="请输入账号"  clearable ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码:" prop="password">
              <template #label>
                <el-tooltip
                  class="box-item"
                  effect="dark"
                  content="保证金监控中心密码"
                  placement="bottom-start"
                >
                <span><el-icon style="font-size: 15px;vertical-align: sub;"><QuestionFilled /></el-icon>密码:</span>
                </el-tooltip>
              </template>
              <el-input v-model="dialogFormData.password" placeholder="请输入密码" type="password" show-password clearable ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="一对一类型:" prop="vipServer"  style="text-align: left;">
              <el-checkbox-group v-model="dialogFormData.vipServer" @change="vipServerChange">
                <template v-for="item in vipServerList" :key="item.id">
                  <el-checkbox :label="item.dicName" :value="item.dicValue" />
                </template>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 3%;">
          <el-button  size="small" @click="visable = false"> 取 消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保 存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addStudent, updateStudent, getByID, createStudentPhone } from '@/api/oss/student.js'
import { selectDictionaryByTypeCodeSet } from '@/api/system/dictionary.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        phone:"",
        name:"",
        code:'',
        wechatID:"",
        wechatNickname:"",
        weMediaPlatform:"",
        weMediaAccount:"",
        platformNickname:"",
        account:"",
        password:"",
        loginPassword:"123456",
        recommendedDate:"",
        accountLevel:'',
        tvAnchor:'',
        weMediaAccount:'',
        groupType:'',
        recommended:[],
        studentManager:[],
        vipServer:[],
        status:1,
      }
export default {
  data() {
    const validatePhone = (rule, value, callback) => {
        if(value) {
            if (value.length != 11) {
                callback(new Error('请输入11位手机号'))
            } else {
                const reg = /^1[3456789]\d{9}$/;
                let flag = reg.test(value)
                if (flag == true) {
                    callback()
                } else {
                    callback(new Error('手机号码格式不正确'))
                }
            }
        } else {
            callback(new Error('手机号码不能为空'))
        }
    }
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { min: 11, max: 11, message: '手机号为11位字符', trigger: ['blur', 'change'] },
        ],
        wechatID: [
          { required: true, message: '请输入微信号', trigger: ['blur', 'change'] },
        ],
        loginPassword: [
          { required: true, message: '请输入登录', trigger: ['blur', 'change'] },
          { min: 6,  message: '登录密码最少为6位', trigger: ['blur', 'change'] },
        ],
      },
      // 自媒体平台字典列表
      weMediaPlatformList:[],
      // 账号等级字典列表
      accountLevelList:[],
      // 自媒体账号字典列表
      weMediaAccountList:[],
      // 一对一服务字典列表
      vipServerList:[],
      // 引流人字典列表
      recommendedList:[],
      // 学员经理字典列表
      studentManagerList:[],
      // 组别字典列表
      groupTypeList:[],
      
    }
  },
  methods: {
    // 一对一服务变更事件处理函数
    vipServerChange(val){
      // 如果选择否之后 , 再选则其他的 , 就去掉否
      if (val.length == 2 && val[0] == 'no') {
        this.dialogFormData.vipServer = [val[1]]
      } else if (val.length > 0 && val.includes("no")) {
        this.dialogFormData.vipServer = ["no"]
      }
    },
    // 获取一个随机手机号
    getRandomPhone(){
      createStudentPhone({}).then(res=>{
        if (res.code == 1) {
          this.dialogFormData.phone = res.data
        }
      })
    },
    openModal(id) {
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        // 获取字典列表入参
        let param = {
          dicTypeCodeSet : ['WeMediaPlatform','AccountLevel','WeMediaAccount','VipServer','Recommended','StudentManager','GroupType']
        }
        selectDictionaryByTypeCodeSet(param).then(res=>{
          if (res.code == 1) {
            // 自媒体平台列表
            this.weMediaPlatformList = res.data.WeMediaPlatform
            // 自媒体账号
            this.weMediaAccountList = res.data.WeMediaAccount
            // 一对一服务类型列表
            this.vipServerList = res.data.VipServer
            // 账号等级
            this.accountLevelList = res.data.AccountLevel
            // 引流人列表
            this.recommendedList = res.data.Recommended
            // 学员经理字典列表
            this.studentManagerList = res.data.StudentManager
            // 组别字典列表
            this.groupTypeList = res.data.GroupType
          }
        })
        if (this.isNotBlank(id)) {
          this.title = '修改学员信息'
          // 获取修改的信息
          getByID({ 'id': id }).then(res => {
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增学员信息'
        }
      })
      this.visable = true
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addStudent(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateStudent(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  /deep/.el-drawer__title {
    text-align: left;
  }
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  /deep/.el-drawer__title {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small) {
    width: 100%;
  }
</style>
